<?php
$AppUI->savePlace();

if (! $canAccess) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// To configure an aditional filter to use in the search string
$additional_filter = '';
// retrieve any state parameters
if (isset( $_GET['where'] )) {
	$AppUI->setState( 'ContIdxWhere', $_GET['where'] );
}
if (isset( $_GET['search_string'] )){
	$AppUI->setState ('ContIdxWhere', '%'.$_GET['search_string']);
	// Added the first % in order to find instrings also
	$additional_filter = "OR contact_first_name like '%{$_GET['search_string']}%'
		OR contact_last_name  like '%{$_GET['search_string']}%'
		OR company_name       like '%{$_GET['search_string']}%'
		OR contact_notes      like '%{$_GET['search_string']}%'
		OR contact_email      like '%{$_GET['search_string']}%'";
}
$tab = isset($_GET['tab']) ? intval($_GET['tab']) : 0;
$where = $AppUI->getState( 'ContIdxWhere' ) ? $AppUI->getState( 'ContIdxWhere' ) : '%';

// optional fields shown in the list (could be modified to allow breif and verbose, etc)
$showfields = array(
	'contact_company' => 'contact_company',
	'company_name' => 'company_name',
	'contact_email' => 'contact_email',
	'contact_phone' => 'contact_phone'
);

require_once $AppUI->getModuleClass('companies');
$company =& new CCompany;
$allowedCompanies = $company->getAllowedSQL($AppUI->user_id);

// assemble the sql statement
$q = new DBQuery;
$q->addQuery('contact_id, contact_order_by');
$q->addQuery($showfields);
$q->addQuery('CONCAT(contact_first_name, " ", contact_last_name) AS contact_name, contact_phone');
$q->addTable('contacts', 'a');
$q->leftJoin('companies', 'b', 'a.contact_company = b.company_id');
$q->addWhere("(contact_first_name LIKE '$where%' $additional_filter)");
$q->addWhere('(contact_private = 0 OR (contact_private = 1 AND contact_owner = '.$AppUI->user_id.')'.
								'  OR contact_owner IS NULL OR contact_owner = 0)');
if (count($allowedCompanies)) {
	$comp_where = implode(' AND ', $allowedCompanies);
	$q->addWhere( '( (' . $comp_where . ') OR contact_company = 0 )' );
}
$contacts = $q->loadHashList('contact_id');

// Let's remove the first '%' that we previously added to ContIdxWhere
$default_search_string = dPformSafe(substr($where, 1, strlen($where)), true);

// setup the title block
$titleBlock = new CTitleBlock( 'Contacts' );
if ($canEdit) {
	$titleBlock->addButton($AppUI->_('CSV Download'), 'index.php?m=contacts&a=csvexport&suppressHeaders=1');
	$titleBlock->addButton($AppUI->_('Import vCard'), 'index.php?m=contacts&a=vcardimport&dialog=0');
}
$titleBlock->show();

?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
	<td class="<?=$tab == 0 ? 'tabon' : 'taboff'?>" style="padding-left: 10px; padding-right: 10px"
		onclick="go('?m=contacts&where=0&tab=0')">All</td>
<?
for ($t = 65; $t < 91; $t++) {
	$c = chr($t);
?>
	<td class="<?=$tab == $t ? 'tabon' : 'taboff'?>" style="padding-left: 4px; padding-right: 5px"
		onclick="go('?m=contacts&where=<?=$c?>&tab=<?=$t?>')">
		<?=$c?>
	</td>
<? } ?>
	<td class="tabox" width="100%">&nbsp;</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="2" class="infopanel">
<tr>
<td width="4px">&nbsp;</td>
<td nowrap="nowrap">
<?=$AppUI->_('Search for')?>&nbsp;
<form action="index.php" method="get">
	<input type="hidden" name="m" value="contacts"/>
</td>
<td>
	<input type="text" class="text" name="search_string" value="<?=$default_search_string?>" />
</td>
<td>
	<input type="image" class="button" src="images/search.gif"/>
</form>
</td>
<td align="right" width="100%">
<? if ($canEdit) { ?>
<input type="button" class="button" value="<?=$AppUI->_('New contacts')?>" onclick="doEditCard(0)"/>
<? } ?>
</td>
<td width="4px">&nbsp;</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="subinfopanel">
<tr>
	<td width="100%" bgcolor="white">
		<div style="width: 100%; padding: 8px">
	<?	foreach ($contacts as $c) {
			$cid = $c['contact_id'];
	?>
		<table cellspacing="0" cellpadding="0" class="vcard">
		<tr class="title">
			<td>
				<div style="float: left">
				<a title="<?=$AppUI->_('View')?>" href="javascript:doViewCard(<?=$cid?>)"><b><?=$c['contact_name']?></b></a>
				</div>
				<div style="float: right">
				<a title="<?=$AppUI->_('Export')?>" href="javascript:doGetCard(<?=$cid?>)"><img src="images/vcard.gif"></a>
				<a title="<?=$AppUI->_('Edit')?>" href="javascript:doEditCard(<?=$cid?>)"><img src="images/edit.gif"></a>
				</div>
			</td>
		</tr>
		<tr class="contents">
			<td valign="top">
				<img src="images/blank.gif" width="200" height="1"/><br/>
			<?
				$company = '';
				foreach ($showfields as $key => $val) {
					if (strlen( $c[$key] ) <= 0) continue;
					switch ($val) {
					case 'contact_email':
						echo '<img src="images/mail.gif">&nbsp;'.
								'<a href="mailto:'.$c[$key].'">'.$c[$key].'</a><br/>';
						break;
					case 'contact_company':
						if (is_numeric($c[$key])) {
							$company = 'javascript:Company('.$c[$key].').show()';
						}
						break;
					case 'company_name':
						echo '<b><a class="company" href="'.$company.'">'.$c[$key].'</a></b><br/>';
						break;
					case 'contact_phone':
						echo '<img src="images/phone.gif">&nbsp;'.$c[$key].'<br />';
						break;
					}
				}
			?>
			</td>
		</tr>
		</table>
	<? } ?>
		</div>
	</td>
</tr>
</table>
